package com.anhuanjia.tools.eao.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.anhuanjia.bean.MhBranch;
import com.anhuanjia.tools.eao.bean.CrmCompany;
import com.anhuanjia.tools.eao.bean.EaoCompanyNumberType;
import com.anhuanjia.tools.eao.bean.EaoCompanyQualifications;
import com.anhuanjia.tools.eao.bean.EaoNumberType;
import com.anhuanjia.tools.eao.bean.EaoTof;
import com.anhuanjia.tools.eao.beanvo.EaoBranchListVo;
import com.anhuanjia.tools.eao.beanvo.EaoBranchTofVo;
import com.anhuanjia.tools.eao.beanvo.EaoBranchVo;
import com.anhuanjia.tools.eao.code.Constant;
import com.efastsoft.bean.CrmPeople;
import com.efastsoft.bean.EcMember;
import com.efastsoft.framework.Member;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.collection.Mapx;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;
import com.efastsoft.framework.utility.StringUtil;
import com.efastsoft.member.bl.MemberBL;
import com.efastsoft.platform.util.NoUtil;

/**
 * 分公司/部门操作
 * 
 * @author Administrator
 * 
 */
@Alias(value = "branch")
public class BranchUI extends UIFacade {

	/**
	 * 获取菜单
	 */
	@Priv(loginType = LoginType.Member)
	public void getMenus() {
		List<EaoBranchVo> hmList = new ArrayList<>();
		String branchInnerCode = "";
		DataTable dt = null;
		if (Member.getType().equals(Constant.ENT)) {
			dt = new Q(
					"select mb.* from mh_branch mb left join ec_member mr on mb.Company = mr.ID  where mr.ID = ? order by mb.companyType desc,mb.AddTime",
					Member.getUID()).fetch();
		} else {
			dt = new Q(
					"select mb.* from mh_branch mb left join mh_resource mr on mb.Company = mr.Company  where mr.Member = ? order by mb.CompanyType desc,mb.AddTime",
					Member.getUID()).fetch();
		}
		if (null == dt || dt.getRowCount() == 0) {
			DataTable company = new Q("select * from Crm_Company where ID = ?", Member.getCompany()).fetch();
			if (company.getRowCount() > 0) {
				Transaction transaction = new Transaction();
				MhBranch mb = new MhBranch();
				mb.setAddTime(new Date());
				mb.setAddUser(Member.getUID());
				mb.setCompany(Member.getCompany());
				mb.setID(NoUtil.getMaxID("MhBranchID"));
				mb.setBranchInnerCode(NoUtil.getMaxNo("MBICode_" + mb.getCompany(), 4));
				mb.setParentInnerCode("0");
				mb.setTreeLevel(1L);
				mb.setName(company.get(0).getString("FullName"));
				mb.setCompanyType("0");
				mb.setIsLeaf("Y");
				mb.setType("0");
				dt = (new Q("select * from Mh_Branch where Company=? order by Orderflag",
						new Object[] { mb.getCompany() })).fetch();
				String orderflag = "";
				if (dt != null && dt.getRowCount() > 0) {
					orderflag = dt.getString(dt.getRowCount() - 1, "OrderFlag");
				} else {
					orderflag = "0";
				}
				transaction.add(new Q("update Mh_Branch set Orderflag=Orderflag+1 where Company=? and Orderflag>?",
						new Object[] { mb.getCompany(), orderflag }));
				mb.setOrderFlag(orderflag + 1);
				transaction.insert(mb);
				if (transaction.commit()) {
					EaoBranchVo hm = new EaoBranchVo();
					hm.setId(mb.getBranchInnerCode());
					hm.setTitle(mb.getName());
					hm.setCompany(mb.getCompany());
					hm.setParentId(mb.getParentInnerCode());
					hm.setCompanyType(mb.getCompanyType());
					$S("branchTree", JSON.toJSON(hm));
				}

			}
		} else {
			for (DataRow dataRow : dt) {
				EaoBranchVo hm = new EaoBranchVo();
				hm.setId(dataRow.getString("BranchInnerCode"));
				hm.setTitle(dataRow.getString("Name"));
				hm.setCompany(dataRow.getLong("Company"));
				hm.setParentId(dataRow.getString("ParentInnerCode"));
				hm.setCompanyType(dataRow.getString("CompanyType"));
				if (Constant.HEAD_OFFICE.equals(hm.getCompanyType())) {
					branchInnerCode = dataRow.getString("ParentInnerCode");
				}
				hmList.add(hm);
			}
			List<EaoBranchVo> hmTree = buildTree(hmList, branchInnerCode);
			$S("branchTree", JSON.toJSON(hmTree));
		}

	}

	public static List<EaoBranchVo> buildTree(List<EaoBranchVo> list, String parentId) {
		List<EaoBranchVo> menus = new ArrayList<EaoBranchVo>();
		for (EaoBranchVo menu : list) {
			String menuId = menu.getId();
			String pid = menu.getParentId();
			if (parentId.equals(pid)) {
				List<EaoBranchVo> menuLists = buildTree(list, menuId);
				menu.setChildren(menuLists);
				menus.add(menu);
			}
		}
		return menus;
	}

	@Priv(loginType = LoginType.Member)
	public void deleteBranch() {
		Transaction trans = new Transaction();
		String Id = $V("Id");
		if (StringUtils.isEmpty(Id)) {
			f("操作失败，请确认操作信息!");
			return;
		}
		trans.add(new Q(" delete from Mh_Branch where ").in("BranchInnerCode", Id).and().eq("Company",
				Member.getCompany()));
		trans.add(new Q(" delete from Mh_Branch where ").in("ParentInnerCode", Id).and().eq("Company",
				Member.getCompany()));
		if (trans.commit()) {
			s("删除成功");
		} else {
			f("删除失败");
		}
	}

	/**
	 * 添加/更改
	 */
	@Priv(loginType = LoginType.Member)
	public void saveOrUpdate() {
		Transaction transaction = new Transaction();
		String id = $V("ID");
		if (StringUtil.isEmpty(id)) {
			MhBranch branch = new MhBranch();
			branch.setValue(this.Request);
			branch.setAddTime(new Date());
			branch.setAddUser(Member.getUID());
			branch.setID(new Date().getTime());
			transaction.insert(branch);
			if (transaction.commit()) {
				s("添加成功");
			} else {
				f("添加失败");
			}
		} else {
			DataTable dt = new Q("select * from Mh_Branch where ID = ?", id).fetch();
			if (dt.getRowCount() != 0) {
				MhBranch branch = new MhBranch();
				branch.setValue(dt.get(0));
				branch.setValue(this.Request);
				branch.setModifyTime(new Date());
				branch.setModifyUser(Member.getUID());
				transaction.update(branch);
				if (transaction.commit()) {
					s("修改成功");
				} else {
					f("修改失败");
				}
			}
		}
	}

	/**
	 * 查询登录人机构信息
	 */
	@Priv(loginType = LoginType.Member)
	public void findMechanism() {
		String id = $V("Id");
		Q q = new Q("SELECT * from crm_company where 1 = 1");
		if (StringUtils.isEmpty(id)) {
			q.and().eq("ID", Member.getCompany());
		} else {
			q.and().eq("ID", id);
		}
		DataTable dt = q.fetch();
		if (dt.getRowCount() != 0) {
			for (DataRow dataRow : dt) {
				CrmCompany company = new CrmCompany();
				company.setValue(dataRow);
				DataTable qualificationTypeTemp = new Q(
						"select ecq.Id,eq.Father,eq.Type from eao_company_qualifications as ecq LEFT JOIN eao_qualifications as eq on ecq.QualificationsId = eq.Id  where ecq.CompanyId = ?",
						Member.getCompany()).fetch();
				String qualificationStrA = Constant.QualificationStrA;
				String qualificationStrB = Constant.QualificationStrB;
				String qualificationStrC = Constant.QualificationStrC;
				if (qualificationTypeTemp.getRowCount() > 0) {
					for (DataRow qualificationType : qualificationTypeTemp) {
						if ("1".equals(qualificationType.getString("Father"))) {
							qualificationStrA = qualificationStrA + qualificationType.getString("Type") + ",";
						} else if ("2".equals(qualificationType.getString("Father"))) {
							qualificationStrB = qualificationStrB + qualificationType.getString("Type") + ",";
						} else if ("3".equals(qualificationType.getString("Father"))) {
							qualificationStrC = qualificationStrC + qualificationType.getString("Type") + ",";
						}
					}
					String str = "";
					if (qualificationStrA.length() > 50) {
						str = str + qualificationStrA.substring(0, qualificationStrA.length() - 1) + "。";
					} else {
						str = str + qualificationStrA + "无。";
					}
					if (qualificationStrB.length() > 50) {
						str = str + qualificationStrB.substring(0, qualificationStrB.length() - 1) + "。";
					} else {
						str = str + qualificationStrB + "无。";
					}
					if (qualificationStrC.length() > 48) {
						str = str + qualificationStrC.substring(0, qualificationStrC.length() - 1) + "。";
					} else {
						str = str + qualificationStrC + "无。";
					}
					company.setQualificationType(str);
				}
				this.Response.put("company", company);
				s("查询成功");
			}
		} else {
			f("没有查到机构信息");
		}
	}

	/**
	 * 修改机构信息
	 */
	@Priv(loginType = LoginType.Member)
	public void updateMechanism() {
		Transaction transaction = new Transaction();
		String id = $V("ID");
		String qualificationTypeStr = $V("QualificationTypeTemp");
		if (StringUtil.isNotEmpty(qualificationTypeStr)) {
			DataTable qualificationTypeTemp = new Q("select * from eao_company_qualifications  where CompanyId = ?",
					Member.getCompany()).fetch();
			if (qualificationTypeTemp.getRowCount() > 0) {
				Transaction tran = new Transaction();
				Q q = new Q("delete from eao_company_qualifications where CompanyId = ?", Member.getCompany());
				tran.add(q);
				tran.commit();
			}
			String[] qualificationIDs = null;
			qualificationIDs = qualificationTypeStr.split(",");
			for (String string : qualificationIDs) {
				EaoCompanyQualifications companyQualifications = new EaoCompanyQualifications();
				companyQualifications.setId(UUID.randomUUID().toString());
				companyQualifications.setCompanyId(Member.getCompany());
				companyQualifications.setQualificationsId(string);
				companyQualifications.setAddUser(Member.getUID());
				companyQualifications.setAddTime(new Date());
				transaction.insert(companyQualifications);
			}
		}
		if (StringUtil.isNotEmpty(id)) {
			DataTable dt = new Q("SELECT * from crm_company  where ID = ?", id).fetch();
			CrmCompany company = new CrmCompany();
			company.setValue(dt.get(0));
			company.setValue(this.Request);
			company.setModifyTime(new Date());
			company.setModifyUser(Member.getUID());
			DataTable dt1 = new Q(
					"SELECT * from Mh_Branch where (ParentInnerCode = 0 or ParentInnerCode = '') and Company = ?", id)
							.fetch();
			if (dt1.getRowCount() != 0) {
				MhBranch branch = new MhBranch();
				branch.setValue(dt1.get(0));
				branch.setName(company.getFullName());
				transaction.update(branch);
			}
			transaction.update(company);
			if (transaction.commit()) {
				s("修改成功");
			}
		} else {
			CrmCompany company = new CrmCompany();
			company.setID(Member.getUID());
			company.setAddUser(Member.getUID());
			company.setAddTime(new Date());
			company.setValue(this.Request);
			transaction.insert(company);
			if (transaction.commit()) {
				s("添加成功");
			}
		}
	}

	/**
	 * 查询登录人信息
	 */
	@Priv(loginType = LoginType.Member)
	public void findMember() {
		String type = Member.getType();
		DataTable people = new Q(" select Mobile , Prop2 , HomePhone from crm_people where Id = ?", Member.getUID()).fetch();
		String Prop2 = null;
		String HomePhone = null;
		String Mobile = null;
		if (people.getRowCount() != 0) {
			Prop2 = people.getString(0, "Prop2");
			HomePhone = people.getString(0, "HomePhone");
			Mobile = people.getString(0, "Mobile");
		}
		if (Constant.ENT.equals(type)) {
			Q q = new Q(" select * from Ec_Member where ID = ?", Member.getUID());
			DataTable dt = q.fetch();
			DataTable dataTable = new Q("select * from eao_images where ParentId = ? and Status = '1'",
					Long.toString(Member.getUID())).fetch();
			if (dt.getRowCount() != 0) {
				for (DataRow dataRow : dt) {
					EcMember member = new EcMember();
					member.setValue(dataRow);
					member.setJob(Constant.ENT_JOB);
					if (dataTable.getRowCount() != 0) {
						member.setProp1(dataTable.getString(0, "ImgUrl"));
					} else {
						member.setProp1(Constant.USER_DEFAULT_HEAD);
					}
					if (StringUtil.isNotEmpty(Prop2)) {
						member.setProp2(Prop2);
					}
					if (StringUtil.isNotEmpty(HomePhone)) {
						member.setTel(HomePhone);
					} else {
						member.setTel(Mobile);
					}
					this.Response.put("member", member);
					s("查询成功");
				}
			} else {
				f("没有查到用户信息");
			}
		} else {
			Q q = new Q(
					" select em.*,br.Name,ep.Position , ei.ImgUrl from Mh_Resource re"
							+ " LEFT JOIN  Ec_Member em on re.Member = em.ID"
							+ " LEFT JOIN  eao_images ei on re.Member = ei.ParentId and ei.Status = '1'"
							+ " LEFT JOIN  Mh_Branch br on re.BranchInnerCode = br.BranchInnerCode"
							+ " LEFT JOIN eao_is_engineer eie on em.ID = eie.Member"
							+ " LEFT JOIN eao_Position ep on ep.Id = eie.IsEngineer" + " where em.ID = ?",
					Member.getUID());
			DataTable dt = q.fetch();
			if (dt.getRowCount() != 0) {
				for (DataRow dataRow : dt) {
					EcMember member = new EcMember();
					member.setValue(dataRow);
					member.setBranchInnerCode(dataRow.getString("Name"));
					member.setJob(dataRow.getString("Position"));
					if (StringUtils.isEmpty(dataRow.getString("ImgUrl"))) {
						member.setProp1(Constant.USER_DEFAULT_HEAD);
					} else {
						member.setProp1(dataRow.getString("ImgUrl"));
					}
					if (StringUtil.isNotEmpty(Prop2)) {
						member.setProp2(Prop2);
					}
					if (StringUtil.isNotEmpty(HomePhone)) {
						member.setTel(HomePhone);
					} else {
						member.setTel(Mobile);
					}
					this.Response.put("member", member);
					s("查询成功");
				}
			} else {
				Q q1 = new Q(" select * from ec_member where ID = ?", Member.getUID());
				DataTable dt1 = q1.fetch();
				DataTable dataTable = new Q("select * from eao_images where ParentId = ? and Status = '1'",
						Long.toString(Member.getUID())).fetch();
				if (dt1.getRowCount() != 0) {
					for (DataRow dataRow : dt1) {
						EcMember member = new EcMember();
						member.setValue(dataRow);
						member.setJob("游客账号");
						if (dataTable.getRowCount() != 0) {
							member.setProp1(dataTable.getString(0, "ImgUrl"));
						} else {
							member.setProp1(Constant.USER_DEFAULT_HEAD);
						}
						if (StringUtil.isNotEmpty(Prop2)) {
							member.setProp2(Prop2);
						}
						if (StringUtil.isNotEmpty(HomePhone)) {
							member.setTel(HomePhone);
						}
						this.Response.put("member", member);
						s("查询成功");
					}
				} else {
					f("没有查到用户信息");
				}
			}
		}

	}

	/**
	 * 修改用户信息
	 */
	@Priv(loginType = LoginType.Member)
	public void updateMember() {
		String id = $V("ID");
		if (StringUtil.isNotEmpty(id)) {
			Transaction transaction = new Transaction();
			DataTable dt = new Q("SELECT * from ec_member  where ID = ?", id).fetch();
			if (dt.getRowCount() != 0) {
				Mapx<String, Object> mdata = new Mapx<String, Object>();
				mdata.put("Tel", $V("Tel"));
				mdata.put("QQ", $V("QQ"));
				mdata.put("Email", $V("Email"));
				mdata.put("ModifyUser", Member.getUID());
				mdata.put("ModifyTime", new Date());

				MemberBL.update(dt.get(0).getString("UserName"), mdata, transaction);
				// ecMember.setWechatOpenID($L("WechatOpenID"));
			}
			String Prop2 = $V("Prop2");
			String HomePhone = $V("Tel");
			if (StringUtil.isNotEmpty(Prop2) || StringUtil.isNotEmpty(HomePhone)) {
				DataTable dataTable = new Q(" select * from crm_people where Id = ?", id).fetch();
				CrmPeople crmPeople = new CrmPeople();
				if (dataTable.getRowCount() != 0) {
					crmPeople.setValue(dataTable.get(0));
					if (StringUtil.isNotEmpty(Prop2)) {
						crmPeople.setProp2(Prop2);
					}
					if (StringUtil.isNotEmpty(HomePhone)) {
						crmPeople.setHomePhone(HomePhone);
					}
					transaction.update(crmPeople);
				} else {
					crmPeople.setID(id);
					crmPeople.setMobile(dt.get(0).getString("Mobile"));
					if (StringUtil.isNotEmpty(Prop2)) {
						crmPeople.setProp2(Prop2);
					}
					if (StringUtil.isNotEmpty(HomePhone)) {
						crmPeople.setHomePhone(HomePhone);
					}
					crmPeople.setAddTime(new Date());
					crmPeople.setAddUser(Member.getRealName());
					transaction.insert(crmPeople);
				}
			}
			if (transaction.commit()) {
				s("修改成功");
			} else {
				f("修改失败");
			}
		} else {
			f("参数错误");
		}
	}

	/**
	 * 项目审核级数
	 */
	@Priv(loginType = LoginType.Member)
	public void findLevel() {
		List<EaoBranchTofVo> branchIsTrue = new ArrayList<>();
		List<EaoBranchTofVo> branchNotTrue = new ArrayList<>();
		Q q = new Q(
				"SELECT mb.BranchInnerCode,mb.`Name`,t.Tof,mb.CompanyType from mh_branch mb LEFT JOIN eao_tof t on t.BranchId = mb.BranchInnerCode and t.CompanyId = mb.Company where 1 = 1 and mb.CompanyType in('0','1')");
		q.and().eq("mb.Company", Member.getCompany());
		q.append(" ORDER BY mb.CompanyType , mb.ModifyTime desc");
		DataTable dt = q.fetch();
		if (dt.getRowCount() != 0) {
			for (DataRow dataRow : dt) {
				EaoBranchTofVo branch = new EaoBranchTofVo();
				branch.setID(dataRow.getString("BranchInnerCode"));
				branch.setName(dataRow.getString("Name"));
				branch.setCompanyType(dataRow.getString("CompanyType"));
				branch.setTof(dataRow.getString("Tof"));
				branchNotTrue.add(branch);
			}
			this.Response.put("branchIsTrue", branchIsTrue);
			this.Response.put("branchNotTrue", branchNotTrue);
			s("查询成功");
		} else {
			f("查询失败");
		}
	}

	/**
	 * 删除审核级数
	 */
	@Priv(loginType = LoginType.Member)
	public void deleteLevel() {
		String id = $V("ID");
		DataTable dt = new Q("SELECT * from Mh_Branch where 1 = 1 and BranchInnerCode = ? and Company = ?", id,
				Member.getCompany()).fetch();
		if (dt.getRowCount() != 0) {
			DataTable dataTable = new Q("SELECT * from eao_tof where 1 = 1 and BranchId = ? and CompanyId = ?", id,
					Member.getCompany()).fetch();
			Transaction transaction = new Transaction();
			MhBranch branch = new MhBranch();
			branch.setValue(dt.get(0));
			if (dataTable.getRowCount() != 0) {
				EaoTof tof = new EaoTof();
				tof.setValue(dataTable.get(0));
				if (Constant.HEAD_OFFICE.equals(branch.getCompanyType())) {
					tof.setTof(Constant.HEAD_OFFICE_AUDIT_LEVEL);
					transaction.update(tof);
				} else if (Constant.BRANCH_OFFICE.equals(branch.getCompanyType())) {
					tof.setTof(Constant.BRANCH_OFFICE_AUDIT_LEVEL);
					transaction.update(tof);
				}
			} else {
				EaoTof tof = new EaoTof();
				tof.setBranchId(id);
				tof.setCompanyId(Long.toString(Member.getCompany()));
				if (Constant.HEAD_OFFICE.equals(branch.getCompanyType())) {
					tof.setTof(Constant.HEAD_OFFICE_AUDIT_LEVEL);
					transaction.insert(tof);
				} else if (Constant.BRANCH_OFFICE.equals(branch.getCompanyType())) {
					tof.setTof(Constant.BRANCH_OFFICE_AUDIT_LEVEL);
					transaction.insert(tof);
				}
			}
			if (transaction.commit()) {
				s("删除成功");
			} else {
				f("删除失败");
			}
		} else {
			f("没有查到本条数据");
		}
	}

	/**
	 * 更改审核级数
	 */
	@Priv(loginType = LoginType.Member)
	public void updateLevel() {
		Transaction transaction = new Transaction();
		String id = $V("Id");
		String tof = $V("Tof");
		DataTable dt = new Q("SELECT * from eao_tof where 1 = 1 and BranchId = ? and CompanyId = ?", id,
				Member.getCompany()).fetch();
		if (dt.getRowCount() != 0) {
			EaoTof branch = new EaoTof();
			branch.setValue(dt.get(0));
			branch.setTof(tof);
			transaction.update(branch);
		} else {
			EaoTof branch = new EaoTof();
			branch.setBranchId(id);
			branch.setCompanyId(Long.toString(Member.getCompany()));
			branch.setTof(tof);
			transaction.insert(branch);
		}
		if (transaction.commit()) {
			s("修改成功");
		} else {
			f("修改失败");
		}
	}

	/**
	 * 查询编号方式
	 */
	@Priv(loginType = LoginType.Member)
	public void findNumberType() {
		DataTable companyNumberType = new Q("select NumberTypeId from eao_company_number_type where CompanyId = ? ",
				Member.getCompany()).fetch();
		DataTable dt = new Q("select * from eao_number_type order by Type").fetch();
		if (dt.getRowCount() != 0) {
			List<EaoNumberType> numberTypeList = new ArrayList<>();
			for (DataRow dataRow : dt) {
				EaoNumberType nt = new EaoNumberType();
				nt.setValue(dataRow);
				numberTypeList.add(nt);
			}
			if (companyNumberType.getRowCount() > 0) {
				this.Response.put("isTrue", companyNumberType.get(0).getString("NumberTypeId"));
			} else {
				this.Response.put("isTrue", "1");// 默认编号规则1
			}
			this.Response.put("numberTypeList", numberTypeList);
			s("查询成功");
		} else {
			f("查询失败");
		}
	}

	/**
	 * 修改编号方式
	 */
	@Priv(loginType = LoginType.Member)
	public void updateNumberType() {
		String id = $V("Id");
		if (StringUtil.isNotEmpty(id)) {
			if ("2".equals(id)) {
				DataTable companyInfo = new Q("select * from eao_company_info where Id = ? ", Member.getCompany())
						.fetch();
				if (companyInfo.getRowCount() == 0) {
					f("本规则不可用，请在会员中心-机构基本资料-机构信息中填写机构名称缩写后使用。");
					return;
				} else if (StringUtil.isEmpty(companyInfo.get(0).getString("NameAbbreviation"))) {
					f("本规则不可用，请在会员中心-机构基本资料-机构信息中填写机构名称缩写后使用。");
					return;
				}
			}
			DataTable companyNumberTypeInfo = new Q("select * from eao_company_number_type where CompanyId = ? ",
					Member.getCompany()).fetch();
			if (companyNumberTypeInfo.getRowCount() > 0) {
				EaoCompanyNumberType companyNumberType = new EaoCompanyNumberType();
				companyNumberType.setValue(companyNumberTypeInfo.get(0));
				companyNumberType.setCompanyId(Member.getCompany());
				companyNumberType.setNumberTypeId(Long.parseLong(id));
				companyNumberType.setModifyUser(Member.getUID());
				companyNumberType.setModifyTime(new Date());
				if (companyNumberType.update()) {
					s("修改成功");
				} else {
					f("修改失败");
				}
			} else {
				EaoCompanyNumberType companyNumberType = new EaoCompanyNumberType();
				companyNumberType.setId(UUID.randomUUID().toString());
				companyNumberType.setCompanyId(Member.getCompany());
				companyNumberType.setNumberTypeId(Long.parseLong(id));
				companyNumberType.setAddUser(Member.getUID());
				companyNumberType.setAddTime(new Date());
				if (companyNumberType.insert()) {
					s("添加成功");
				} else {
					f("添加失败");
				}
			}

		} else {
			f("参数错误");
		}
	}

	/**
	 * 查询人员
	 */
	@Priv(loginType = LoginType.Member)
	public void findBranchMember() {
		String branchInnerCode = $V("BranchInnerCode");
		String searchName = $V("SearchName");
		Q q = new Q(
				"select em.ID,em.RealName from Mh_Resource mr left join Ec_Member em on em.ID = mr.Member where 1=1");
		if (StringUtil.isNotEmpty(branchInnerCode)) {
			q.and().eq("mr.BranchInnerCode", branchInnerCode);
		}
		if (StringUtil.isNotEmpty(searchName)) {
			q.and().append(" em.RealName like '%?%'", searchName);
		}
		DataTable dt = q.fetch();
		if (dt.getRowCount() > 0) {
			List<Map<String, String>> memberList = new ArrayList<>();
			for (DataRow dataRow : dt) {
				Map<String, String> map = new HashMap<>();
				map.put("id", dataRow.getString("ID"));
				map.put("realName", dataRow.getString("RealName"));
				memberList.add(map);
			}
			this.Response.put("memberList", memberList);
		}
	}

	/**
	 * 查询下拉框人员
	 */
	@Priv(loginType = LoginType.Member)
	public void likeBranchMember() {
		Q q = new Q(
				"select em.ID,em.RealName from ec_member em ,mh_resource mr where em.ID = mr.Member and mr.Company = ?",
				Member.getCompany());
		DataTable dt = q.fetch();
		List<Map<String, String>> memberList = new ArrayList<>();
		if (dt.getRowCount() > 0) {
			for (DataRow dataRow : dt) {
				Map<String, String> map = new HashMap<>();
				map.put("id", dataRow.getString("ID"));
				map.put("realName", dataRow.getString("RealName"));
				memberList.add(map);
			}
		}
		this.Response.put("selectMember", memberList);
	}

	/**
	 * 重置密码
	 */
	@Priv(loginType = LoginType.Member)
	public void resetPassword() {
		Transaction t = new Transaction();
		String id = $V("Id");
		if(StringUtils.isEmpty(id)) {
			f("操作失败");
		}
		DataTable dt = new Q("select * from ec_member where ID = ?", id).fetch();
		if (dt.getRowCount() > 0) {
			for (DataRow dataRow : dt) {
				Mapx<String, Object> mdata = new Mapx<String, Object>();
				mdata.put("Password", "123456");
				MemberBL.update(dataRow.getString("UserName"), mdata, t);
				if (t.commit()) {
					s("重置密码成功");
				} else {
					f("重置密码失败");
				}
			}
		} else {
			f("查找用户失败");
		}
	}

	@Priv(loginType = LoginType.Member)
	public void findName() {
		String ids = $V("Ids");
		if(StringUtils.isEmpty(ids)) {
			f("操作失败");
		}
		DataTable dt = new Q(" select ID,RealName from Ec_Member where 1 = 1").and().in("ID", ids).fetch();
		List<Map<String, Object>> listMap = new ArrayList<>();
		if (dt.getRowCount() > 0) {
			for (DataRow dataRow : dt) {
				Map<String, Object> map = new HashMap<>();
				map.put("id", dataRow.getLong("ID"));
				map.put("name", dataRow.getString("RealName"));
				listMap.add(map);
			}
			this.Response.put("userName", listMap);
		} else {
			this.Response.put("userName", listMap);
			f("查询失败");
		}
	}

	/**
	 * 查询公司部门
	 */
	@Priv(loginType = LoginType.Member)
	public void findBranch() {
		String BranchInnerCode = $V("ParentInnerCode");
		if (StringUtil.isNotEmpty(BranchInnerCode)) {
			DataTable branchListTemp = new Q(
					" select * from Mh_Branch where ParentInnerCode = ? and Company = ? and CompanyType = '2' order by AddTime desc",
					BranchInnerCode, Member.getCompany()).fetch();
			if (branchListTemp.getRowCount() > 0) {
				List<EaoBranchListVo> branchList = new ArrayList<>();
				for (DataRow dataRow : branchListTemp) {
					EaoBranchListVo branch = new EaoBranchListVo();
					branch.setId(dataRow.getLong("ID"));
					branch.setBranchInnerCode(dataRow.getString("BranchInnerCode"));
					branch.setCompany(dataRow.getLong("Company"));
					branch.setCompanyType(dataRow.getString("CompanyType"));
					branch.setName(dataRow.getString("Name"));
					branch.setParentInnerCode(dataRow.getString("ParentInnerCode"));
					branchList.add(branch);
				}
				DataTable branchTemp = new Q(
						" select CompanyType from Mh_Branch where BranchInnerCode = ? and Company = ?", BranchInnerCode,
						Member.getCompany()).fetch();
				if (branchTemp.getRowCount() > 0) {
					$S("companyType", branchTemp.get(0).getString("CompanyType"));
				}
				$S("branchForCompany", branchList);
			} else {
				f("没有部门信息");
			}
		} else {
			f("参数错误");
		}
	}
}